-- SkyPetShopM
-- Created by huanghr
-- 天空营地

module("SkyPetShopM", package.seeall);

-- 配置
local skyPetShopConfig = {};
local petList       = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    -- 转换下pet_shop配置表
    local sky_pet_shop_CSV = get_sky_pet_shop_CSV();
    for i = 1, #sky_pet_shop_CSV do
        -- 解析出商品信息
        skyPetShopConfig[sky_pet_shop_CSV[i].id] = sky_pet_shop_CSV[i];
        petList[sky_pet_shop_CSV[i].pet_id] = skyPetShopConfig[sky_pet_shop_CSV[i].id];
    end


    sky_pet_shop_CSV = {};
end

-- 初始化
function init()
    loadCsv();
end

-- 获取商品
function query(goodsId, path)
    -- 返回商品
    local m = skyPetShopConfig[goodsId];

    if nil == m then
        return nil;
    end

    if (path == "" or path == nil) then
        return m;
    else
        return m[path];
    end
end

-- 检索商品价格
function queryPrice(goodsId)
    if skyPetShopConfig[goodsId] then
        return skyPetShopConfig[goodsId].price;
    else
        return nil;
    end
end

-- 返回天空营地冈布奥信息
function querySkySlimes()
    return skyPetShopConfig;
end

-- 通过冈布奥ID获得对应的冈布奥的信息
function queryPriceByPetId(petId)
    if petList[petId] then
        return petList[petId].price;
    end

    return nil;
end

-- 获得天空之城可招募的冈布奥(返回商品Id)
function getCanRecruitIds()
    local ret = {};
    local infoList = skyPetShopConfig;
    for _, info in pairs(infoList) do
        local petId = info.pet_id;
        if not PetM.getMyPet(petId) then
            table.insert(ret, info.id);
        end
    end
    return ret;
end

-- 能否兑换冈布奥
function canExchangeGoods(id)
    local priceInfo = queryPrice(id);

    -- 未获得对应价格
    if "nil" == priceInfo then
        return nil;
    end

    if ItemM.getAmount(ME.user, priceInfo[2]) < priceInfo[3] then
        -- 用于兑换的荣誉碎片不足
        return nil;
    end

    if ME.user.pets[query(id).pet_id] then
        -- 宠物已激活
        return nil;
    end

    return true;
end

-- 获得天空营地可以出售的冈布奥碎片
function getCanSellScraps()
    -- 若玩家已有该冈布奥
    local ret = {}
    for _, info in pairs(skyPetShopConfig) do
        if PetM.getMyPet(info.pet_id) then
            table.insert(ret, info);
        end
    end

    return ret;
end

-- 判断能否购买遗物
function canBuyScrap(goodsId, num)
    local cost = query(goodsId, "scrap_price");

    if cost[1] == 1 then
        return ItemM.getAmount(ME.user, cost[2]) >= cost[3] * num;
    elseif cost[1] == 2 then
        return ME.user.dbase:query(cost[2], 0) >= cost[3] * num;
    end

    return false;
end

-- 购买遗物
function buyScrap(goodsId, num)
    -- 能否购买
    if not canBuyScrap(goodsId, num) then
        return false;
    end

    local cost = query(goodsId, "scrap_price");

    if cost[1] == 1 then
        if not ItemM.costAmount(ME.user, cost[2], cost[3] * num) then
            return false;
        end
    elseif cost[1] == 2 then
        if not ME.user:costAttrib(cost[2], cost[3] * num) then
            return false;
        end
    end

    -- 奖励
    local bonus = query(goodsId, "sell_scrap");

    BonusM.doBonus({bonus[1], bonus[2], bonus[3] * num}, "buy_sky_pet_scrap");

    -- 抛出购买商品的事件
    EventMgr.fire(event.BUY_SLIME_SCRAP, { ["goodsId"] = goodsId, ["amount"] = bonus[3] * num});
    return true;
end